import 'package:flutter_wan_android/api/api_paths.dart';
import 'package:flutter_wan_android/bean/article_list_bean.dart';
import 'package:flutter_wan_android/bean/banner_bean.dart';
import 'package:flutter_wan_android/bean/project_list_bean.dart';
import 'package:flutter_wan_android/bean/project_tree_bean.dart';
import 'package:flutter_wan_android/bean/wechat_bean.dart';

import 'http_utils.dart';

class ApiServer {


  static Future<List<BannerBean>> getBannerData() async {
    return await HttpUtils().get<List>(ApiPaths.BANNER).then((list) {
      return list.map((i) => BannerBean.fromJson(i)).toList();
    }).catchError((error) {
      return error;
    });
  }

  static Future<ArticleListBean> getArticleList(int pageIndex) async {
    return await HttpUtils()
        .get<Map<String, dynamic>>(ApiPaths.ARTICLE_LIST(pageIndex))
        .then((map) {
      return ArticleListBean.fromJson(map);
    }).catchError((error) {
      return error;
    });
  }

  ///当cid不传时，默认是获取完整项目数据
  static Future<ProjectListBean> getProjectList(int pageIndex,{int cid=294}) async {
    return await HttpUtils()
        .get<Map<String, dynamic>>(ApiPaths.PROJECT_LIST(pageIndex, cid))
        .then((map) {
      return ProjectListBean.fromJson(map);
    }).catchError((error) {
      return error;
    });
  }


  static Future<ProjectListBean> getHomeProjectList(int pageIndex) async {
    return await HttpUtils()
        .get<Map<String, dynamic>>(ApiPaths.HOME_PROJECT_LIST(pageIndex))
        .then((map) {
      return ProjectListBean.fromJson(map);
    }).catchError((error) {
      return error;
    });
  }


  static Future<List<ProjectTreeBean>> getProjectTree() async {
    return await HttpUtils().get<List>(ApiPaths.PROJECT_TREE).then((list){
      return list.map((i) => ProjectTreeBean.fromJson(i)).toList();
    }).catchError((error){
      return error;
    });
  }


  static Future<List<WeChatBean>> getWeChatNum() async {
      return await HttpUtils().get<List>(ApiPaths.WECHAT_NO).then((list){
          return list.map((item) => WeChatBean.fromJson(item)).toList();
      }).catchError((error){
        return error;
      });
  }


  static Future<ArticleListBean> getWXArticleList(int pageIndex,int id) async {
    return await HttpUtils()
        .get<Map<String, dynamic>>(ApiPaths.WX_ARTICLE(pageIndex,id))
        .then((map) {
      return ArticleListBean.fromJson(map);
    }).catchError((error) {
      return error;
    });
  }

}
